import time
from datetime import datetime

from library import vk1640b

MAX_DIGITS = 4

NUMBERS = {
    "0": 0x3F,
    "1": 0x06,
    "2": 0x5B,
    "3": 0x4F,
    "4": 0x66,
    "5": 0x6D,
    "6": 0x7D,
    "7": 0x07,
    "8": 0x7F,
    "9": 0x6F,
    "A": 0x77,
    "b": 0x7C,
    "C": 0x39,
    "d": 0x5E,
    "E": 0x79,
    "F": 0x71,
    "-": 0x40,
    ".": 0x80,
}


def display(numb):
    data_packages = [(0, NUMBERS[str(numb % 10000 // 1000)]), (1, NUMBERS[str(numb % 1000 // 100)]),
                     (2, NUMBERS[str(numb % 100 // 10)]), (3, NUMBERS[str(numb % 10)])]
    vk1640b.send_fixed_address_data(data_packages)


if __name__ == '__main__':
    vk1640b.init()
    vk1640b.clear_display(MAX_DIGITS)
    try:
        while True:
            # 显示年
            display(datetime.today().year)
            time.sleep(1)
            # 显示月日
            display(datetime.today().month * 100 + datetime.today().day)
            time.sleep(1)
            # 显示时分
            display(datetime.now().time().hour * 100 + datetime.now().time().minute)
            time.sleep(1)
    finally:
        vk1640b.clear_display(MAX_DIGITS)
        vk1640b.cleanup()
